package cn.tedu.tmall.admin.mall.controller;

import cn.tedu.tmall.admin.mall.pojo.param.CategoryAddNewParam;
import cn.tedu.tmall.admin.mall.pojo.param.GoodsAddNewParam;
import cn.tedu.tmall.admin.mall.pojo.vo.CategoryStandardVO;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsListItemVO;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsStandardVO;
import cn.tedu.tmall.admin.mall.service.IGoodsService;
import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@Slf4j
@RestController
@RequestMapping("/goods")
@Api(tags = "02. 商品管理")
@Validated
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @PostMapping("/add-new")
    @ApiOperation("发布商品")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@ApiIgnore CurrentPrincipal currentPrincipal,
                             @Valid GoodsAddNewParam goodsAddNewParam) {
        goodsService.addNew(currentPrincipal,goodsAddNewParam);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询商品")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id",value = "类别ID",required = true,dataType = "long")
    public JsonResult getStandardById(@Range(min = 1,message = "根据ID获取商品失败,请提交有效的商品ID值")
                          @PathVariable Long id) {
        GoodsStandardVO queryResult = goodsService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("")
    @ApiOperation("查询商品列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({@ApiImplicitParam(name="page", value = "页码",defaultValue = "1",paramType = "query",dataType ="int" )})
    public JsonResult list(@Range(min = 1,message = "请提交有效的页码值!") Integer page){
        Integer pageNum = page == null ? 1 : page;
        PageData<GoodsListItemVO> pageData = goodsService.list(pageNum,page);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/list-by-category")
    @ApiOperation("根据类别查询商品列表")
    @ApiOperationSupport(order = 421)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "商品类别ID", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int")
    })
    public JsonResult listByCategory(@Range(message = "请提交有效的商品类别ID值！") Long categoryId,
                                     @Range(min = 1, message = "请提交有效的页码值！") Integer page) {
        Integer pageNum = page == null ? 1 : page;
        PageData<GoodsListItemVO> pageData = goodsService.listByCategory(categoryId, pageNum,page);
        return JsonResult.ok(pageData);
    }

    @PostMapping("/{id:[0-9]+]}/check-state/pass")
    @ApiOperation("审核通过商品")
    @ApiOperationSupport(order = 310)
    public JsonResult passCheck(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                @PathVariable @Range(min = 1,message = "请提交有效的商品ID值!")Long id,
                                @NotBlank(message = "请提交审核备注信息") String remarks) {
        goodsService.passCheck(currentPrincipal, id, remarks);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+]}/check-state/reiect")
    @ApiOperation("审核通过商品")
    @ApiOperationSupport(order = 311)
    public JsonResult rejectCheck(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                @PathVariable @Range(min = 1,message = "请提交有效的商品ID值!")Long id,
                                @NotBlank(message = "请提交审核备注信息") String remarks) {
        goodsService.rejectCheck(currentPrincipal, id, remarks);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+]}/put-on")
    @ApiOperation("上架商品")
    @ApiOperationSupport(order = 320)
    public JsonResult putOn(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                  @PathVariable @Range(min = 1,message = "请提交有效的商品ID值!")Long id) {
        goodsService.putOn(currentPrincipal, id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+]}/pull-off")
    @ApiOperation("下架商品")
    @ApiOperationSupport(order = 321)
    public JsonResult pullOff(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                            @PathVariable @Range(min = 1,message = "请提交有效的商品ID值!")Long id) {
        goodsService.pullOff(currentPrincipal, id);
        return JsonResult.ok();
    }
}
